package com.example.controller;

import com.example.annotation.RateLimit;
import com.example.common.Result;
import com.example.entity.User;
import com.example.service.UserService;
import com.example.service.UserDeviceService;
import com.example.vo.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
@Api(tags = "用户管理接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserDeviceService userDeviceService;

    @ApiOperation(value = "发送注册验证码", notes = "向指定手机号发送注册验证码")
    @ApiImplicitParam(name = "phone", value = "手机号码", required = true, paramType = "query")
    @PostMapping("/register/code")
    public ApiResponse<Boolean> sendRegisterCode(@RequestParam String phone) {
        try {
            boolean success = userService.sendRegisterCode(phone);
            return ApiResponse.success(success);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @ApiOperation(value = "发送登录验证码", notes = "向指定手机号发送登录验证码")
    @ApiImplicitParam(name = "phone", value = "手机号码", required = true, paramType = "query")
    @PostMapping("/login/sendCode")
    public ApiResponse<Boolean> sendLoginCode(@RequestParam String phone) {
        try {
            boolean success = userService.sendLoginCode(phone);
            return ApiResponse.success(success);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @ApiOperation(value = "发送重置密码验证码", notes = "向指定手机号发送重置密码验证码")
    @ApiImplicitParam(name = "phone", value = "手机号码", required = true, paramType = "query")
    @PostMapping("/reset/code")
    public ApiResponse<Boolean> sendResetCode(@RequestParam String phone) {
        try {
            boolean success = userService.sendResetCode(phone);
            return ApiResponse.success(success);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }

    @ApiOperation(value = "验证码登录", notes = "使用手机号和验证码进行登录")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "phone", value = "手机号码", required = true, paramType = "query"),
        @ApiImplicitParam(name = "code", value = "验证码", required = true, paramType = "query")
    })
    @PostMapping("/login/code")
    public ApiResponse<User> loginByCode(@RequestParam String phone, @RequestParam String code) {
        try {
            User user = userService.loginByCode(phone, code);
            return ApiResponse.success(user);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }

    @ApiOperation(value = "密码登录", notes = "使用手机号和密码进行登录")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "phone", value = "手机号码", required = true, paramType = "query"),
        @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "query")
    })
    @PostMapping("/login/password")
    public ApiResponse<User> loginByPassword(@RequestParam String phone, @RequestParam String password) {
        try {
            User user = userService.loginByPassword(phone, password);
            return ApiResponse.success(user);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }

    @ApiOperation(value = "用户注册", notes = "注册新用户")
    @PostMapping("/register")
    public ApiResponse<Boolean> register(@Valid @RequestBody User user) {
        try {
            boolean success = userService.register(user);
            return ApiResponse.success(success);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @ApiOperation(value = "验证重置密码请求", notes = "验证重置密码的验证码")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "phone", value = "手机号码", required = true, paramType = "query"),
        @ApiImplicitParam(name = "code", value = "验证码", required = true, paramType = "query")
    })
    @PostMapping("/reset/verify")
    public ApiResponse<Boolean> verifyResetRequest(@RequestParam String phone, @RequestParam String code) {
        try {
            boolean success = userService.verifyResetRequest(phone, code);
            return ApiResponse.success(success);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @ApiOperation(value = "重置密码", notes = "提交新密码")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "phone", value = "手机号码", required = true, paramType = "query"),
        @ApiImplicitParam(name = "newPassword", value = "新密码", required = true, paramType = "query")
    })
    @PostMapping("/reset/password")
    public ApiResponse<Boolean> resetPassword(@RequestParam String phone, @RequestParam String newPassword) {
        try {
            boolean success = userService.resetPassword(phone, newPassword);
            return ApiResponse.success(success);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @ApiOperation(value = "检查手机号是否已注册", notes = "检查手机号是否已被注册")
    @ApiImplicitParam(name = "phone", value = "手机号码", required = true, paramType = "query")
    @GetMapping("/check/phone")
    public ApiResponse<Map<String, Boolean>> checkPhoneExists(@RequestParam String phone) {
        try {
            boolean exists = userService.checkPhoneExists(phone);
            Map<String, Boolean> result = new HashMap<>();
            result.put("exists", exists);
            return ApiResponse.success(result);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @ApiOperation(value = "退出登录", notes = "用户退出登录")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query")
    @PostMapping("/logout")
    public ApiResponse<Boolean> logout(@RequestParam Long userId) {
        try {
            boolean success = userService.logout(userId);
            return ApiResponse.success(success);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }

    @ApiOperation(value = "用户注销", notes = "注销用户账号")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "phone", value = "手机号码", required = true, paramType = "query"),
        @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "query")
    })
    @PostMapping("/unregister")
    public ApiResponse<Boolean> unregister(@RequestParam String phone, @RequestParam String password) {
        try {
            boolean success = userService.unregister(phone, password);
            return ApiResponse.success(success);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }

    @ApiOperation(value = "绑定设备", notes = "通过SN码绑定设备")
    @PostMapping("/bindDevice")
    public ApiResponse<Boolean> bindDevice(@RequestParam Long userId, @RequestParam String sn) {
        try {
            boolean success = userDeviceService.bindDevice(userId, sn);
            return ApiResponse.success(success);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
} 